package com.jiangjiang.eshop.common.json;

import org.springframework.stereotype.Component;

/**
 * @Author Jianghaowen
 * @Date 2021/9/23
 * @Description: json字段值提取语法的解析器
 * @Version V1.0
 **/
public class JsonExtractSyntaxParser {
    private static final String JSON_EXTRACT_SYNTAX_SPLITTER = "/";

    /**
     * 解析json字段值提取语法
     * @param jsonExtractSyntax json字段值提取语法
     * @return 抽象语法树
     * @throws Exception
     */
    public static JsonExpression parse(String jsonExtractSyntax){
        String[] elements = jsonExtractSyntax.split(JSON_EXTRACT_SYNTAX_SPLITTER);

        JsonExpression rootJsonExpression = null;
        CompositeJsonExpression currentJsonExpression = null;

        for (int i = 0; i < elements.length; i++) {
            if (i == 0 ) {
                if(elements.length == 1){
                    rootJsonExpression = new LeafJsonExpression(elements[i]);
                } else {
                    rootJsonExpression = new CompositeJsonExpression(elements[i]);
                    currentJsonExpression = (CompositeJsonExpression) rootJsonExpression;
                }

            }else if(i < elements.length - 1){
                CompositeJsonExpression childJsonExpression = new CompositeJsonExpression(elements[i]);
                currentJsonExpression.setChildJsonExpression(childJsonExpression);
                currentJsonExpression = childJsonExpression;

            } else {
                LeafJsonExpression leafJsonExpression = new LeafJsonExpression(elements[i]);
                currentJsonExpression.setChildJsonExpression(leafJsonExpression);
            }
        }
        return rootJsonExpression;
    }

}
